package com.paireach.szwolaiyun.newFragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.activity.Activity_A;
import com.paireach.szwolaiyun.activity.Activity_C;
import com.paireach.szwolaiyun.activity.Activity_D;
import com.paireach.szwolaiyun.sz_common_widget.fragment.BaseFragment;
import com.paireach.szwolaiyun.sz_util.LogUtil;
import com.paireach.szwolaiyun.sz_util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import szwolaiyun.paireach.com.szwolaizhuang.model.business.DriveOrder;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpErrorEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpSuccessEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.requestTag.RequestTag;
import szwolaiyun.paireach.com.szwolaizhuang.net.WayBillRequest;
import szwolaiyun.paireach.com.szwolaizhuang.net.json.JsonPaser;

/**
 * 主页面  implements View.OnClickListener
 */
public class HomeFragment extends BaseFragment {

    private static final String TAG = "HomeFragment";
    private View rootView;
    private Button button;
    private LinearLayout orderDetailLayout;
    private TextView noOrderTextView;
    //====================data
    private String name, phone;
    private DriveOrder driveOrder;
    private SwipeRefreshLayout swipeRefreshLayout;
    private TextView count1;
    private TextView count2;
    private TextView count3;

    public static HomeFragment getInstance(String name, String phone) {
        HomeFragment homeFragment = new HomeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        bundle.putString("phone", phone);
        homeFragment.setArguments(bundle);
        return homeFragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initTopFragment();
//        initView();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            name = bundle.getString("name");
            phone = bundle.getString("phone");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_home_v2, container, false);
        count1 = (TextView) rootView.findViewById(R.id.tv_order_count);
        count2 = (TextView) rootView.findViewById(R.id.tv_start_transpost_order_count);
        count3 = (TextView) rootView.findViewById(R.id.tv_sign_transpost_order_count);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        //根据手机号获取第一个可接的运单
        showProgressDialog();
//        new WayBillRequest().loadFirstAvailableOrder(phone);
        new WayBillRequest().getOrderCount(phone);
    }

    //顶部的用户名+手机号+异常电话
    private void initTopFragment() {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        HomeTopFragment topFragment = HomeTopFragment.getInstance(name, phone);
        transaction.add(R.id.main_frame, topFragment, HomeTopFragment.TAG);
        transaction.commit();
    }

//    private void initView() {
//        button = (Button) rootView.findViewById(R.id.bt_accept);
//        button.setOnClickListener(this);
//        orderDetailLayout = (LinearLayout) rootView.findViewById(R.id.have_order_layout);
//        noOrderTextView = (TextView) rootView.findViewById(R.id.no_order);
//    }

//    @Override
//    public void onClick(View v) {
//        if (v == button) {
//            Bundle bundle = new Bundle();
//            bundle.putSerializable("DriveOrder", driveOrder);
//            int stateCode = driveOrder.getOrderStateCode();
//            if (stateCode <= 220) {//司机未接运单前的状态
//                //屬於界面A的，詳細看設計圖
//                Intent intentA = new Intent(getActivity(), Activity_A.class);
//                intentA.putExtras(bundle);
//                startActivity(intentA);
//            } else if (stateCode == 224 || stateCode == 226 || stateCode == 236 || stateCode == 238) {
//                //屬於界面D的，詳細看設計圖
//                Intent intentD = new Intent(getActivity(), Activity_D.class);
//                intentD.putExtras(bundle);
//                startActivity(intentD);
//            } else {
//                //屬於界面C的，詳細看設計圖
//                Intent intentC = new Intent(getActivity(), Activity_C.class);
//                intentC.putExtras(bundle);
//                startActivity(intentC);
//            }
//        }
//    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if (event.getRequestTag() == RequestTag.ORDER_COUNT) {
            dismissProgressDialog();
            showToast(event.getErrorMessage());
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
//        if (event.getRequestTag() == RequestTag.GET_FIRST_AVAILABLE_WAY_BILL) {
//            dismissProgressDialog();
//            JsonPaser.parseFirstAvailableOrder(event.getJson());
//            Log.d(TAG, "主页：event.getJson() = " + event.getJson());
//        }
        if (event.getRequestTag() == RequestTag.ORDER_COUNT) {
            LogUtil.d(TAG, "SUCCESS" + event.getJson());
            dismissProgressDialog();
            try {
                JSONObject object = new JSONObject(event.getJson());
                String loadResult = object.getString("loadResult");
                if (loadResult.equals("0")) {
                    ToastUtil.showShortToast(getContext(), "没有数据");
                } else {
                    int allOrderNum = object.getInt("allOrderNum");
                    int partOrderNum = object.getInt("partOrderNum");
                    int signOrderNum = object.getInt("signOrderNum");
                    count1.setText("总运单数量:" + allOrderNum);
                    count2.setText("送货在途运单数量:" + partOrderNum);
                    count3.setText("已签收运单数量:" + signOrderNum);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                ToastUtil.showShortToast(getContext(), "解析数据错误");
            }
        }
    }
//
//    @Override
//    public void applicationEvent(AppEvent event) {
//        super.applicationEvent(event);
//        if (event.getTag().equals("first_available_order_info")) {
//            if (event.getCode() == 0) {
//                setViewState((DriveOrder) event.getObj1());
//            } else if (event.getCode() == 1) {
//                orderDetailLayout.setVisibility(View.INVISIBLE);
//                noOrderTextView.setVisibility(View.VISIBLE);
//                button.setClickable(false);
//            } else {
//                showToast(event.getExtraInfo());
//            }
//        }
//    }

//    private void setViewState(DriveOrder order) {
//        this.driveOrder = order;
//        orderDetailLayout.setVisibility(View.VISIBLE);
//        noOrderTextView.setVisibility(View.INVISIBLE);
//        button.setClickable(true);
//        ((TextView) rootView.findViewById(R.id.tv_home_ordercode)).setText(order.getOrderNo());
//        ((TextView) rootView.findViewById(R.id.tv_ordertime)).setText(order.getPlaceTime());
//        ((TextView) rootView.findViewById(R.id.tv_originname)).setText(order.getStartPlaceName());
//        ((TextView) rootView.findViewById(R.id.tv_origin_address)).setText(order.getStartPlaceDetailAddress());
//        ((TextView) rootView.findViewById(R.id.tv_endname)).setText(order.getEndPlaceName());
//        ((TextView) rootView.findViewById(R.id.tv_end_address)).setText(order.getEndPlaceDetailAddress());
//        int code = order.getOrderStateCode();
//        if (code < 220) {
//            button.setText("接收运单");
//        } else {
//            button.setText("下一步");
//        }
//    }
}
